import { _decorator, Asset, Component, Node, Size, Vec2, Vec3, view } from 'cc';
import { Chess_Const } from './ChessConst';
import { ResMgr } from '../Managers/ResMgr';
import { AB_Name } from '../Data/GameConstant';
import { ChessAI } from './ChessAI';
import { ChessMan } from './ChessMan';
import { ChessUIPanel } from './ChessUIPanel';
const { ccclass, property } = _decorator;

@ccclass('ChessCommon')
export class ChessCommon {
    
    /**
     * 
     */
    public static Keys = {
        "c0":"c","c1":"c",
        "m0":"m","m1":"m",
        "x0":"x","x1":"x",
        "s0":"s","s1":"s",
        "j0":"j",
        "p0":"p","p1":"p",
        "z0":"z","z1":"z","z2":"z","z3":"z","z4":"z","z5":"z",

        "C0":"c","C1":"C",
        "M0":"M","M1":"M",
        "X0":"X","X1":"X",
        "S0":"S","S1":"S",
        "J0":"J",
        "P0":"P","P1":"P",
        "Z0":"Z","Z1":"Z","Z2":"Z","Z3":"Z","Z4":"Z","Z5":"Z",
    }
    /**
     * 棋盘
     */
    public static initMap:string[][] = [
        ['C0','M0','X0','S0','J0','S1','X1','M1','C1'],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,'P0',    ,    ,    ,    ,    ,'P1',    ],
        ['Z0',    ,'Z1',    ,'Z2',    ,'Z3',    ,'Z4'],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        ['z0',    ,'z1',    ,'z2',    ,'z3',    ,'z4'],
        [    ,'p0',    ,    ,    ,    ,    ,'p1',    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        ['c0','m0','x0','s0','j0','s1','x1','m1','c1']
    ];
         
    /// <summary>
    /// 棋盘1
    /// </summary>
    public static initMap1:string[][] = [
        [    ,    ,    ,, "J0"   ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    ,"c0",    ,    ,    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    ,	  ,    ,    ,    ],
        [    ,    ,    ,    ,"s0",    ,    ,"C0",    ],
        [    ,    ,    ,"s1",    ,"j0",    ,    ,    ]
    ];
    /// <summary>
    /// 棋盘2
    /// </summary>
    public static initMap2:string[][] = [
        [    ,    ,    ,, "J0"   ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    , ,    ,    ,    ],
        [    ,    ,    ,    ,    ,"z0",    ,    ,    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,    ,    ,    ,    ,	  ,    ,    ,    ],
        [    ,    ,    ,    ,    ,    ,    ,    ,    ],
        [    ,    ,    , "j0"   ,,    ,    ,    ]
    ];

    /**
     * 象棋价值
     */
    public static value = {
        /**
         * 车价值
         */
        c:[
            [206, 208, 207, 213, 214, 213, 207, 208, 206],
            [206, 212, 209, 216, 233, 216, 209, 212, 206],
            [206, 208, 207, 214, 216, 214, 207, 208, 206],
            [206, 213, 213, 216, 216, 216, 213, 213, 206],
            [208, 211, 211, 214, 215, 214, 211, 211, 208],
            
            [208, 212, 212, 214, 215, 214, 212, 212, 208],
            [204, 209, 204, 212, 214, 212, 204, 209, 204],
            [198, 208, 204, 212, 212, 212, 204, 208, 198],
            [200, 208, 206, 212, 200, 212, 206, 208, 200],
            [194, 206, 204, 212, 200, 212, 204, 206, 194]
        ],
        
        /**
         * 马价值
         */
        m:[
            [90, 90, 90, 96, 90, 96, 90, 90, 90],
            [90, 96,103, 97, 94, 97,103, 96, 90],
            [92, 98, 99,103, 99,103, 99, 98, 92],
            [93,108,100,107,100,107,100,108, 93],
            [90,100, 99,103,104,103, 99,100, 90],
            
            [90, 98,101,102,103,102,101, 98, 90],
            [92, 94, 98, 95, 98, 95, 98, 94, 92],
            [93, 92, 94, 95, 92, 95, 94, 92, 93],
            [85, 90, 92, 93, 78, 93, 92, 90, 85],
            [88, 85, 90, 88, 90, 88, 90, 85, 88]
        ],
        
        /**
         * 相(象)价值
         */
        x:[
            [0, 0,20, 0, 0, 0,20, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0,23, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0,20, 0, 0, 0,20, 0, 0],
            
            [0, 0,20, 0, 0, 0,20, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [18,0, 0, 0,23, 0, 0, 0,18],
            [0, 0, 0, 0, 0, 0, 0, 0, 0], 
            [0, 0,20, 0, 0, 0,20, 0, 0]
        ],
        
        /**
         * 士价值
         */
        s:[
            [0, 0, 0,20, 0,20, 0, 0, 0],
            [0, 0, 0, 0,23, 0, 0, 0, 0],
            [0, 0, 0,20, 0,20, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0,20, 0,20, 0, 0, 0],
            [0, 0, 0, 0,23, 0, 0, 0, 0], 
            [0, 0, 0,20, 0,20, 0, 0, 0]
        ],
        
        /**
         * 将(帅)价值
         */
        j:[
            [0, 0, 0, 8888, 8888, 8888, 0, 0, 0],
            [0, 0, 0, 8888, 8888, 8888, 0, 0, 0], 
            [0, 0, 0, 8888, 8888, 8888, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 8888, 8888, 8888, 0, 0, 0],
            [0, 0, 0, 8888, 8888, 8888, 0, 0, 0], 
            [0, 0, 0, 8888, 8888, 8888, 0, 0, 0]
        ],
        
        /**
         * 炮价值
         */
        p:[
            
            [100, 100,  96, 91,  90, 91,  96, 100, 100],
            [ 98,  98,  96, 92,  89, 92,  96,  98,  98],
            [ 97,  97,  96, 91,  92, 91,  96,  97,  97],
            [ 96,  99,  99, 98, 100, 98,  99,  99,  96],
            [ 96,  96,  96, 96, 100, 96,  96,  96,  96], 
            
            [ 95,  96,  99, 96, 100, 96,  99,  96,  95],
            [ 96,  96,  96, 96,  96, 96,  96,  96,  96],
            [ 97,  96, 100, 99, 101, 99, 100,  96,  97],
            [ 96,  97,  98, 98,  98, 98,  98,  97,  96],
            [ 96,  96,  97, 99,  99, 99,  97,  96,  96]
        ],
        
        /**
         * 卒价值
         */
        z:[
            [ 9,  9,  9, 11, 13, 11,  9,  9,  9],
            [19, 24, 34, 42, 44, 42, 34, 24, 19],
            [19, 24, 32, 37, 37, 37, 32, 24, 19],
            [19, 23, 27, 29, 30, 29, 27, 23, 19],
            [14, 18, 20, 27, 29, 27, 20, 18, 14],
            
            [ 7,  0, 13,  0, 16,  0, 13,  0,  7],
            [ 7,  0,  7,  0, 15,  0,  7,  0,  7], 
            [ 0,  0,  0,  0,  0,  0,  0,  0,  0],
            [ 0,  0,  0,  0,  0,  0,  0,  0,  0],
            [ 0,  0,  0,  0,  0,  0,  0,  0,  0]
        ],
    };

    /**
     * 棋子们
     */
    public static args = {
        //红子 中文/图片地址/阵营/权重
        "c":{text:"车", img:"r_c", my:1 ,bl:"c", value:ChessCommon.value.c},
        "m":{text:"马", img:"r_m", my:1 ,bl:"m", value:ChessCommon.value.m},
        "x":{text:"相", img:"r_x", my:1 ,bl:"x", value:ChessCommon.value.x},
        "s":{text:"仕", img:"r_s", my:1 ,bl:"s", value:ChessCommon.value.s},
        "j":{text:"将", img:"r_j", my:1 ,bl:"j", value:ChessCommon.value.j},
        "p":{text:"炮", img:"r_p", my:1 ,bl:"p", value:ChessCommon.value.p},
        "z":{text:"兵", img:"r_z", my:1 ,bl:"z", value:ChessCommon.value.z},

        //蓝子
        "C":{text:"車", img:"b_c", my:-1 ,bl:"c", value:ChessCommon.arr2CloneReverse(ChessCommon.value.c)},
        "M":{text:"馬", img:"b_m", my:-1 ,bl:"m", value:ChessCommon.arr2CloneReverse(ChessCommon.value.m)},
        "X":{text:"象", img:"b_x", my:-1 ,bl:"x", value:ChessCommon.value.x},
        "S":{text:"士", img:"b_s", my:-1 ,bl:"s", value:ChessCommon.value.s},
        "J":{text:"帅", img:"b_j", my:-1 ,bl:"j", value:ChessCommon.value.j},
        "P":{text:"炮", img:"b_p", my:-1 ,bl:"p", value:ChessCommon.arr2CloneReverse(ChessCommon.value.p)},
        "Z":{text:"卒", img:"b_z", my:-1 ,bl:"z", value:ChessCommon.arr2CloneReverse(ChessCommon.value.z)},
    };

    //棋子能走的着点
    public static bylaw = {
        /**
         * 车
         */
        c : function(x:number, y:number, map:string[][], my:number):number[][]{
            var d:number[][]=[];
            //左侧检索
            for (var i=x-1; i>= 0; i--){
                if (map[y][i]) {
                    if (ChessUIPanel.Instance.mans[map[y][i]].my!=my) d.push([i,y]);
                    break
                }else{
                    d.push([i,y])	
                }
            }
            //右侧检索
            for (var i=x+1; i <= 8; i++){
                if (map[y][i]) {
                    if (ChessUIPanel.Instance.mans[map[y][i]].my!=my) d.push([i,y]);
                    break
                }else{
                    d.push([i,y])	
                }
            }
            //上检索
            for (var i = y-1 ; i >= 0; i--){
                if (map[i][x]) {
                    if (ChessUIPanel.Instance.mans[map[i][x]].my!=my) d.push([x,i]);
                    break
                }else{
                    d.push([x,i])	
                }
            }
            //下检索
            for (var i = y+1 ; i<= 9; i++){
                if (map[i][x]) {
                    if (ChessUIPanel.Instance.mans[map[i][x]].my!=my) d.push([x,i]);
                    break
                }else{
                    d.push([x,i])	
                }
            }
            return d;
        },
        //马
        m : function(x:number, y:number, map:string[][], my:number):number[][]{
            var d:number[][] = [];
            //1点
            if ( y-2>= 0 && x+1<= 8 && !ChessUIPanel.Instance.map[y-1][x] &&(!ChessUIPanel.Instance.mans[map[y-2][x+1]] || ChessUIPanel.Instance.mans[map[y-2][x+1]].my!=my)) d.push([x+1,y-2]);
            //2点
            if ( y-1>= 0 && x+2<= 8 && !ChessUIPanel.Instance.map[y][x+1] &&(!ChessUIPanel.Instance.mans[map[y-1][x+2]] || ChessUIPanel.Instance.mans[map[y-1][x+2]].my!=my)) d.push([x+2,y-1]);
            //4点
            if ( y+1<= 9 && x+2<= 8 && !ChessUIPanel.Instance.map[y][x+1] &&(!ChessUIPanel.Instance.mans[map[y+1][x+2]] || ChessUIPanel.Instance.mans[map[y+1][x+2]].my!=my)) d.push([x+2,y+1]);
            //5点
            if ( y+2<= 9 && x+1<= 8 && !ChessUIPanel.Instance.map[y+1][x] &&(!ChessUIPanel.Instance.mans[map[y+2][x+1]] || ChessUIPanel.Instance.mans[map[y+2][x+1]].my!=my)) d.push([x+1,y+2]);
            //7点
            if ( y+2<= 9 && x-1>= 0 && !ChessUIPanel.Instance.map[y+1][x] &&(!ChessUIPanel.Instance.mans[map[y+2][x-1]] || ChessUIPanel.Instance.mans[map[y+2][x-1]].my!=my)) d.push([x-1,y+2]);
            //8点
            if ( y+1<= 9 && x-2>= 0 && !ChessUIPanel.Instance.map[y][x-1] &&(!ChessUIPanel.Instance.mans[map[y+1][x-2]] || ChessUIPanel.Instance.mans[map[y+1][x-2]].my!=my)) d.push([x-2,y+1]);
            //10点
            if ( y-1>= 0 && x-2>= 0 && !ChessUIPanel.Instance.map[y][x-1] &&(!ChessUIPanel.Instance.mans[map[y-1][x-2]] || ChessUIPanel.Instance.mans[map[y-1][x-2]].my!=my)) d.push([x-2,y-1]);
            //11点
            if ( y-2>= 0 && x-1>= 0 && !ChessUIPanel.Instance.map[y-1][x] &&(!ChessUIPanel.Instance.mans[map[y-2][x-1]] || ChessUIPanel.Instance.mans[map[y-2][x-1]].my!=my)) d.push([x-1,y-2]);

            return d;
        },
        //相
        x : function (x:number, y:number, map:string[][], my:number):number[][]{
            var d:number[][]=[];
            if (my===1){ //红方
                //4点半
                if ( y+2<= 9 && x+2<= 8 && !ChessUIPanel.Instance.map[y+1][x+1] && (!ChessUIPanel.Instance.mans[map[y+2][x+2]] || ChessUIPanel.Instance.mans[map[y+2][x+2]].my!=my)) d.push([x+2,y+2]);
                //7点半
                if ( y+2<= 9 && x-2>= 0 && !ChessUIPanel.Instance.map[y+1][x-1] && (!ChessUIPanel.Instance.mans[map[y+2][x-2]] || ChessUIPanel.Instance.mans[map[y+2][x-2]].my!=my)) d.push([x-2,y+2]);
                //1点半
                if ( y-2>= 5 && x+2<= 8 && !ChessUIPanel.Instance.map[y-1][x+1] && (!ChessUIPanel.Instance.mans[map[y-2][x+2]] || ChessUIPanel.Instance.mans[map[y-2][x+2]].my!=my)) d.push([x+2,y-2]);
                //10点半
                if ( y-2>= 5 && x-2>= 0 && !ChessUIPanel.Instance.map[y-1][x-1] && (!ChessUIPanel.Instance.mans[map[y-2][x-2]] || ChessUIPanel.Instance.mans[map[y-2][x-2]].my!=my)) d.push([x-2,y-2]);
            }else{
                //4点半
                if ( y+2<= 4 && x+2<= 8 && !ChessUIPanel.Instance.map[y+1][x+1] && (!ChessUIPanel.Instance.mans[map[y+2][x+2]] || ChessUIPanel.Instance.mans[map[y+2][x+2]].my!=my)) d.push([x+2,y+2]);
                //7点半
                if ( y+2<= 4 && x-2>= 0 && !ChessUIPanel.Instance.map[y+1][x-1] && (!ChessUIPanel.Instance.mans[map[y+2][x-2]] || ChessUIPanel.Instance.mans[map[y+2][x-2]].my!=my)) d.push([x-2,y+2]);
                //1点半
                if ( y-2>= 0 && x+2<= 8 && !ChessUIPanel.Instance.map[y-1][x+1] && (!ChessUIPanel.Instance.mans[map[y-2][x+2]] || ChessUIPanel.Instance.mans[map[y-2][x+2]].my!=my)) d.push([x+2,y-2]);
                //10点半
                if ( y-2>= 0 && x-2>= 0 && !ChessUIPanel.Instance.map[y-1][x-1] && (!ChessUIPanel.Instance.mans[map[y-2][x-2]] || ChessUIPanel.Instance.mans[map[y-2][x-2]].my!=my)) d.push([x-2,y-2]);
            }
            return d;
        },
        //士
        s : function (x:number, y:number, map:string[][], my:number):number[][]{
            var d:number[][] = [];
            if (my===1){ //红方
                //4点半
                if ( y+1<= 9 && x+1<= 5 && (!ChessUIPanel.Instance.mans[map[y+1][x+1]] || ChessUIPanel.Instance.mans[map[y+1][x+1]].my!=my)) d.push([x+1,y+1]);
                //7点半
                if ( y+1<= 9 && x-1>= 3 && (!ChessUIPanel.Instance.mans[map[y+1][x-1]] || ChessUIPanel.Instance.mans[map[y+1][x-1]].my!=my)) d.push([x-1,y+1]);
                //1点半
                if ( y-1>= 7 && x+1<= 5 && (!ChessUIPanel.Instance.mans[map[y-1][x+1]] || ChessUIPanel.Instance.mans[map[y-1][x+1]].my!=my)) d.push([x+1,y-1]);
                //10点半
                if ( y-1>= 7 && x-1>= 3 && (!ChessUIPanel.Instance.mans[map[y-1][x-1]] || ChessUIPanel.Instance.mans[map[y-1][x-1]].my!=my)) d.push([x-1,y-1]);
            }else{
                //4点半
                if ( y+1<= 2 && x+1<= 5 && (!ChessUIPanel.Instance.mans[map[y+1][x+1]] || ChessUIPanel.Instance.mans[map[y+1][x+1]].my!=my)) d.push([x+1,y+1]);
                //7点半
                if ( y+1<= 2 && x-1>= 3 && (!ChessUIPanel.Instance.mans[map[y+1][x-1]] || ChessUIPanel.Instance.mans[map[y+1][x-1]].my!=my)) d.push([x-1,y+1]);
                //1点半
                if ( y-1>= 0 && x+1<= 5 && (!ChessUIPanel.Instance.mans[map[y-1][x+1]] || ChessUIPanel.Instance.mans[map[y-1][x+1]].my!=my)) d.push([x+1,y-1]);
                //10点半
                if ( y-1>= 0 && x-1>= 3 && (!ChessUIPanel.Instance.mans[map[y-1][x-1]] || ChessUIPanel.Instance.mans[map[y-1][x-1]].my!=my)) d.push([x-1,y-1]);
            }
            return d;
        },
        //将
        j : function (x:number, y:number, map:string[][], my:number):number[][]{
            var d:number[][] = [];
            var isNull=(function (y1,y2){
                var y11 = ChessUIPanel.Instance.mans["j0"].y;
                var x11 = ChessUIPanel.Instance.mans["J0"].x;
                var y22 = ChessUIPanel.Instance.mans["J0"].y;
                for (var i=y11-1; i>y22; i--){
                    if (map[i][x11]) return false;
                }
                return true;
            })();
            
            if (my===1){ //红方
                //下
                if ( y+1<= 9  && (!ChessUIPanel.Instance.mans[map[y+1][x]] || ChessUIPanel.Instance.mans[map[y+1][x]].my!=my)) d.push([x,y+1]);
                //上
                if ( y-1>= 7 && (!ChessUIPanel.Instance.mans[map[y-1][x]] || ChessUIPanel.Instance.mans[map[y-1][x]].my!=my)) d.push([x,y-1]);
                //老将对老将的情况
                if (ChessUIPanel.Instance.mans["j0"].x == ChessUIPanel.Instance.mans["J0"].x &&isNull) d.push([ChessUIPanel.Instance.mans["J0"].x, ChessUIPanel.Instance.mans["J0"].y]);
                // console.log("my::"+my+" x::"+x+" y::"+y);
            }else{
                //下
                if ( y+1<= 2  && (!ChessUIPanel.Instance.mans[map[y+1][x]] || ChessUIPanel.Instance.mans[map[y+1][x]].my!=my)) d.push([x,y+1]);
                //上
                if ( y-1>= 0 && (!ChessUIPanel.Instance.mans[map[y-1][x]] || ChessUIPanel.Instance.mans[map[y-1][x]].my!=my)) d.push([x,y-1]);
                //老将对老将的情况
                if ( ChessUIPanel.Instance.mans["j0"].x == ChessUIPanel.Instance.mans["J0"].x &&isNull) d.push([ChessUIPanel.Instance.mans["j0"].x,ChessUIPanel.Instance.mans["j0"].y]);
            }
            //右
            if ( x+1<= 5  && (!ChessUIPanel.Instance.mans[map[y][x+1]] || ChessUIPanel.Instance.mans[map[y][x+1]].my!=my)) d.push([x+1,y]);
            //左
            if ( x-1>= 3 && (!ChessUIPanel.Instance.mans[map[y][x-1]] || ChessUIPanel.Instance.mans[map[y][x-1]].my!=my))d.push([x-1,y]);
            return d;
        },
        //炮
        p : function (x:number, y:number, map:string[][], my:number):number[][]{
            var d:number[][] = [];
            //左侧检索
            var n=0;
            for (var i=x-1; i>= 0; i--){
                if (map[y][i]) {
                    if (n==0){
                        n++;
                        continue;
                    }else{
                        if (ChessUIPanel.Instance.mans[map[y][i]].my!=my) d.push([i,y]);
                        break	
                    }
                }else{
                    if(n==0) d.push([i,y])	
                }
            }
            //右侧检索
            var n=0;
            for (var i=x+1; i <= 8; i++){
                if (map[y][i]) {
                    if (n==0){
                        n++;
                        continue;
                    }else{
                        if (ChessUIPanel.Instance.mans[map[y][i]].my!=my) d.push([i,y]);
                        break	
                    }
                }else{
                    if(n==0) d.push([i,y])	
                }
            }
            //上检索
            var n=0;
            for (var i = y-1 ; i >= 0; i--){
                if (map[i][x]) {
                    if (n==0){
                        n++;
                        continue;
                    }else{
                        if (ChessUIPanel.Instance.mans[map[i][x]].my!=my) d.push([x,i]);
                        break	
                    }
                }else{
                    if(n==0) d.push([x,i])	
                }
            }
            //下检索
            var n=0;
            for (var i = y+1 ; i<= 9; i++){
                if (map[i][x]) {
                    if (n==0){
                        n++;
                        continue;
                    }else{
                        if (ChessUIPanel.Instance.mans[map[i][x]].my!=my) d.push([x,i]);
                        break	
                    }
                }else{
                    if(n==0) d.push([x,i])	
                }
            }
            return d;
        },
        //卒
        z : function (x:number, y:number, map:string[][], my:number):number[][]{
            var d:number[][] = [];
            if (my===1){ //红方
                //上
                if ( y-1>= 0 && (!ChessUIPanel.Instance.mans[map[y-1][x]] || ChessUIPanel.Instance.mans[map[y-1][x]].my!=my)) d.push([x,y-1]);
                //右
                if ( x+1<= 8 && y<=4  && (!ChessUIPanel.Instance.mans[map[y][x+1]] || ChessUIPanel.Instance.mans[map[y][x+1]].my!=my)) d.push([x+1,y]);
                //左
                if ( x-1>= 0 && y<=4 && (!ChessUIPanel.Instance.mans[map[y][x-1]] || ChessUIPanel.Instance.mans[map[y][x-1]].my!=my))d.push([x-1,y]);
            }else{
                //下
                if ( y+1<= 9  && (!ChessUIPanel.Instance.mans[map[y+1][x]] || ChessUIPanel.Instance.mans[map[y+1][x]].my!=my)) d.push([x,y+1]);
                //右
                if ( x+1<= 8 && y>=6  && (!ChessUIPanel.Instance.mans[map[y][x+1]] || ChessUIPanel.Instance.mans[map[y][x+1]].my!=my)) d.push([x+1,y]);
                //左
                if ( x-1>= 0 && y>=6 && (!ChessUIPanel.Instance.mans[map[y][x-1]] || ChessUIPanel.Instance.mans[map[y][x-1]].my!=my))d.push([x-1,y]);
            }
            return d;
        }
    };

    /**
     * 二维数组克隆
     * @param arr 
     * @returns 
     */
    public static arr2Clone(arr:any[][]):any[][]{
        var newArr:any[][] = [];
        for(var i = 0; i < arr.length; i++){
            newArr.push(arr[i].slice());
        }
        return newArr;
    }

    /**
     * 二维数组克隆 (黑子为红字价值位置的倒置)
     * @param arr 
     * @returns 
     */
    public static arr2CloneReverse(arr:number[][]):number[][]{
        return ChessCommon.arr2Clone(arr).reverse();
    }
    /**
     * 判断是否有效行列
     * @param x 
     * @param y 
     * @returns 
     */
    public static IsValideRowCol(x:number, y:number):boolean{
        if(x >= 0 && x <= 8 && y >= 0 && y <= 9){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 判断key是否有效
     * @param key 
     * @returns 
     */
    public static IsValideKey(key:string):boolean{
        if(null != key && key.length > 0){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 判断key是否为将（帅）
     * @param clearKey 棋子key
     * @returns 
     */
    public static IsJiangKey(clearKey:string):boolean{
        if(clearKey == "j0" || clearKey == "J0"){
            return true;
        }
        return false;
    }

    /**
     * 获取[x,y] value
     * @param x 
     * @param y 
     * @returns 
     */
    public static GetXYValue(x:number, y:number):number{
        return y * 10 + x;
    }

    //#region 坐标转换
    /**
     * UI坐标 转换 棋盘坐标(行,列)
     * x:0~8 (共计9列， 从左到右依次递增) 
     * y:0~9 (共计10行， 从上到下依次递增)
     */
    public static convertPosToChessRowCol(uipos:Vec2, offPos:Vec3):Vec2{
        var viewSize:Size = view.getVisibleSize();
        var localX = uipos.x - (viewSize.width - Chess_Const.With) / 2 - Chess_Const.PointStartX - offPos.x;
        var localY = uipos.y - (viewSize.height - Chess_Const.Height) / 2 - Chess_Const.PointStartY - offPos.y;

        var x = Math.round(localX / Chess_Const.SpaceX);
        var y = Chess_Const.ROW - 1 - Math.round(localY / Chess_Const.SpaceY);
        return new Vec2(x, y);
    }

    //帅（红方）的坐标：（0，-257） 大写 (w:530, h:567)
    //将（黑方）的坐标：（0，259）小写
    /**
     * 换棋盘坐标(行,列) 转行 为棋盘UI坐标
     * x:0~8 (共计9列， 从左到右依次递增) 
     * y:0~9 (共计10行， 从上到下依次递增)
     */
    public static convertRowColToChessPos(x:number, y:number):Vec3{
        var x = Math.round(x * Chess_Const.SpaceX  - (Chess_Const.COL - 1) * Chess_Const.SpaceX * 0.5);
        var y = Math.round((0 - y) * Chess_Const.SpaceY + Chess_Const.SpaceY * (Chess_Const.ROW - 1) * 0.5);
        return new Vec3(x, y, 0);
    }
    //#endregion

    /**
     * 把坐标生成着法
     * @param map 
     * @param x 
     * @param y 
     * @param newX 
     * @param newY 
     * @returns 
     */
    public static createMove(map:string[][], x:number, y:number, newX:number, newY:number):string{
        var h="";
        var man = ChessUIPanel.Instance.mans[map[y][x]];
        h += man.text;
        map[newY][newX] = map[y][x];
        map[y][x] = "";
        if (man.my===1){
            var mumTo=["一","二","三","四","五","六","七","八","九","十"];	
            newX=8-newX;
            h+= mumTo[8-x];
            if (newY > y) {
                h+= "退";
                if (man.pater == "m" || man.pater == "s" || man.pater == "x"){
                    h+= mumTo[newX];
                }else {
                    h+= mumTo[newY - y -1];
                }
            }else if (newY < y) {
                h+= "进";
                if (man.pater == "m" || man.pater == "s" || man.pater == "x"){
                    h+= mumTo[newX];
                }else {
                    h+= mumTo[y - newY -1];
                }
            }else {
                h+= "平";
                h+= mumTo[newX];
            }
        }else{
            var mumTo=["１","２","３","４","５","６","７","８","９","10"];
            h+= mumTo[x];
            if (newY > y) {
                h+= "进";
                if (man.pater == "M" || man.pater == "S" || man.pater == "X"){
                    h+= mumTo[newX];
                }else {
                    h+= mumTo[newY - y-1];
                }
            }else if (newY < y) {
                h+= "退";
                if (man.pater == "M" || man.pater == "S" || man.pater == "X"){
                    h+= mumTo[newX];
                }else {
                    h+= mumTo[y - newY-1];
                }
            }else {
                h+= "平";
                h+= mumTo[newX];
            }
        }
        return h;
    }
    /**
     * 棋盘
     */
    public static gambit:string[];

    /**
     * 初始化
     */
    public static Init(){
        //加载棋谱
        ResMgr.Instance.loadResByAsset(AB_Name.Configs, "gambit_txt", (asset)=>{
            if(asset){
                var gambit_txt:string = asset.text;
                ChessCommon.gambit = gambit_txt.split(" ");
                console.log("gambit_txt load succ::" + ChessCommon.gambit.length);
            }else{
                console.error("gambit_txt load failed!");
            }
        });
    }
}

